package demo.refresh;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class PanelTest {

    public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

class TestFrame extends JFrame {
    private static final long serialVersionUID = -1616986901248246594L;

    public TestFrame() {
        setTitle("test ");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        writer wt = new writer();
        Container contentPane = getContentPane();
        show = new JTextPane();
        contentPane.add(show, BorderLayout.SOUTH);

        wt.start();
    }

    public void showText() {
        setDocs("asdfasdfsadf ", Color.red, false, 10);
    }

    public void setDocs(String str, Color col, boolean bold, int fontSize) {
        SimpleAttributeSet attrSet = new SimpleAttributeSet();
        StyleConstants.setForeground(attrSet, col);
        if (bold == true) {
            StyleConstants.setBold(attrSet, true);
        }
        StyleConstants.setFontSize(attrSet, fontSize);
        insert(str, attrSet);
    }

    public void insert(String str, AttributeSet attrSet) {
        Document doc = show.getDocument();
        str = str + "\n ";
        try {
            doc.insertString(doc.getLength(), str, attrSet);

            show.repaint();
        }
        catch (BadLocationException e) {
            System.out.println("BadLocationException:   " + e);
        }
    }

    private class writer extends Thread {
        @Override
        public void run() {
            while (true) {
                TestFrame.this.showText();
                try {
                    sleep(1000);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public static final int DEFAULT_WIDTH = 300;

    public static final int DEFAULT_HEIGHT = 300;

    private JTextPane show;

}